package com.think.conf;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/***
 * 配置多个数据源
 *
 * @author thinkstop
 *
 */
@Configuration
@Slf4j
public class DataSourceConfig {
	
	@Autowired
	private DataBase1Properties dataBase1Properties;
	
	@Autowired
	private DataBase2Properties dataBase2Properties;

    @Bean(name = "primaryDataSource")
    @Primary
    public DataSource primaryDataSource() {
//        return DataSourceBuilder.create().build();
	    log.info("primaryDataSource初始化----111111");
//        HikariDataSource dataSource = new HikariDataSource();
	    DruidDataSource dataSource = new DruidDataSource();
	    dataSource.setUrl(dataBase1Properties.getUrl());
	    dataSource.setUsername(dataBase1Properties.getUsername());
	    dataSource.setPassword(dataBase1Properties.getPassword());
	    dataSource.setDriverClassName(dataBase1Properties.getDriverClassName());
	    return dataSource;
    }

    @Bean(name = "secondaryDataSource")
    public DataSource secondaryDataSource() {
	    log.info("secondaryDataSource初始化----111111");
//        HikariDataSource dataSource = new HikariDataSource();
	    DruidDataSource dataSource = new DruidDataSource();
	    dataSource.setUrl(dataBase2Properties.getUrl());
	    dataSource.setUsername(dataBase2Properties.getUsername());
	    dataSource.setPassword(dataBase2Properties.getPassword());
	    dataSource.setDriverClassName(dataBase2Properties.getDriverClassName());
	    return dataSource;
    }

}
